package mobi.greenplate.client.connect;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;

final class HttpClientTemplateContext {

    public static HttpClientTemplateContext createContext(String url, Map<String, String> params, String stringEntity, ResponseHandler responseHandler) {
        return new HttpClientTemplateContext(url, convertMap(params), convertString(stringEntity), responseHandler);
    }

    private static HttpParams convertMap(Map<String, String> params) {
        final HttpParams httpParams = new BasicHttpParams();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            httpParams.setParameter(entry.getKey(), entry.getValue());
        }
        return httpParams;
    }

    private static StringEntity convertString(String stringEntityString) {
        try {
            return new StringEntity(stringEntityString);
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        }
    }

    private HttpClientTemplateContext(String url, HttpParams params, StringEntity stringEntity, ResponseHandler responseHandler) {
        this.url = url;
        this.params = params;
        this.stringEntity = stringEntity;
        this.responseHandler = responseHandler;
    }
    private final String url;
    private final HttpParams params;
    private final StringEntity stringEntity;
    private final ResponseHandler responseHandler;

    public final String getUrl() {
        return url;
    }

    public final HttpParams getParams() {
        return params;
    }

    public final HttpEntity getHttpEntity() {
        return stringEntity;
    }

    public final ResponseHandler getResponseHandler() {
        return responseHandler;
    }
}
